// 表单自定义验证生成器
const validatorCreator = (label, { reg, min, max, required, message } = {}) => {
  return (rules, value, callback) => {
    if (required && !value) {
      return callback(`${label}不能为空`);
    }
    if (!value) {
      return callback();
    }
    if (min && value.length < min) {
      return callback(`${label}长度最小为${min}`);
    }
    if (max && value.length > max) {
      return callback(`${label}长度最大为${max}`);
    }
    if (reg && !reg.test(value)) {
      return callback(message || `请输入正确格式的${label}`);
    }
    return callback();
  };
};

export default validatorCreator;
